#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk
pygtk.require('2.0')

import gtk
import gtk.glade
import gobject

from xml.dom import minidom

import os, sys

class Previewer(gtk.Window):
    def __get_topmost_widgets(self, glade_file):
        widgets = []

        f = open(glade_file)
        xmldoc = minidom.parseString(f.read())

        root = xmldoc.documentElement

        for e in [ c for c in root.childNodes if c.nodeName == u'widget']:
            widgets.append(e.getAttribute('id'))

        return widgets

    def __on_preview(self, button, data):
        widget = self.__glade.get_widget(data)
        widget.connect('destroy-event', widget.hide)
        widget.show_all()

    def __load_glade(self, glade_file):
        for button in self.__buttons:
            button.destroy()

        #self.__buttons = []

        print glade_file
        self.__glade = gtk.glade.XML(glade_file)

        for w in self.__get_topmost_widgets(glade_file):
            button = gtk.Button(w)
            button.set_use_underline(False)
            button.show()
            button.connect('clicked', self.__on_preview, w)
            self.__body_vbox.pack_start(button, False, False, padding=4)
            self.__buttons.append(button)

    def __on_open_glade_clicked(self, button):
        self.__open_glade_file()

    def __open_glade_file(self):
        file_dialog = gtk.FileChooserDialog(
            title = "Open a glade file",
            action = gtk.FILE_CHOOSER_ACTION_OPEN,
            buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))

        ffilter = gtk.FileFilter()
        ffilter.set_name("Glade Files")
        ffilter.add_pattern("*.glade")
        ffilter.add_pattern("*.ui")
        file_dialog.add_filter(ffilter)

        if self.__current_glade:
            file_dialog.set_current_folder_uri('file://' + os.path.dirname(self.__current_glade))

        def run_file_dialog(file_dialog):
            ''' True: continue, False: retry '''
            if file_dialog.run() == gtk.RESPONSE_OK:
                self.__current_glade = file_dialog.get_filename()
                try:
                    self.__load_glade(self.__current_glade)
                except:
                    md = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK)
                    md.set_markup('The file is not a valid libglade format')
                    md.run()
                    md.destroy()
                    return False
            return True

        while not run_file_dialog(file_dialog):
            run_file_dialog(file_dialog)

        file_dialog.destroy()

    def __init__(self):
        self.__buttons = []

        self.__current_glade = None

        gtk.Window.__init__(self)
        self.connect('destroy', gtk.main_quit)
        self.set_border_width(8)
        self.set_size_request(200, 200)

        self.__body_vbox = gtk.VBox()
        self.add(self.__body_vbox)

        self.__open_glade = gtk.Button('_Open glade file')
        self.__body_vbox.pack_start(self.__open_glade, False, False, padding=4)
        self.__open_glade.connect('clicked', self.__on_open_glade_clicked)

        self.__body_vbox.pack_start(gtk.HSeparator(), False, False, padding=4)

        if len(sys.argv) > 1:
            self.__load_glade(sys.argv[1])
        self.show_all()

if __name__ == '__main__':
    main_window = Previewer()
    gtk.main()

